//
//  AppDelegate.m
//  UI调研NSFileManager
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [self dirHome];
      [self dirDoc];
      [self dirLib];
      [self dicCache];
      [self dirTmp];
      [self createDir];
      [self writeFile];
      [self readFile];
      [self fileAttriutes];
      [self deleteFile];
      return YES;
}
//获取沙盒根路径
- (void)dirHome {
      NSString *dicHome = NSHomeDirectory();
      NSLog(@"app_Home: %@", dicHome);
}
//获取Documents目录路径
- (NSString *)dirDoc {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSLog(@"app_home_doc :%@", documentsDirectory);
      return documentsDirectory;
}
//获取Library目录路径
- (void)dirLib {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
      NSString *libraryDirectory = [paths objectAtIndex:0];
      NSLog(@"app_home_lib : %@", libraryDirectory);
}
//获取Cache目录路径
- (void)dicCache {
      NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
      NSString *cachePath = [cacPath objectAtIndex:0];
      NSLog(@"app_home_lib_cache : %@", cachePath);
}
//获取Tmp目录路径
-  (void)dirTmp {
      NSString *tmpDirectory = NSTemporaryDirectory();
      NSLog(@"app_home_tmp : %@", tmpDirectory);
}
//创建文件夹
- (void)createDir {
      NSString *documentsPath = [self dirDoc];
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
      BOOL res = [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
      if (res) {
            NSLog(@"文件创建成功");
      }else {
            NSLog(@"文件创建失败");
      }
}
//写数据到文件
- (void)writeFile {
      NSString *documentsPath = [self dirDoc];
      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
      NSString *content = @"测试写入内容";
      BOOL res = [content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
      if (res) {
            NSLog(@"文件写入成功");
      }else {
            NSLog(@"文件写入失败");
      }
}
//读文件数据
- (void)readFile {
      NSString *documentsPath = [self dirDoc];
      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
      NSString *content = [NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
      NSLog( @"文件读取成功 : %@", content);
}
//文件属性
- (void)fileAttriutes {
      NSString *documentsPath = [self dirDoc];
      NSString *textDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSString *testPath = [textDirectory stringByAppendingPathComponent:@"test.txt"];
      NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
      NSArray *keys;
      id key, value ;
      keys = [fileAttributes allKeys];
      NSInteger count = [keys count];
      for (int i = 0; i < count; i++) {
            key = [keys objectAtIndex:i];
            value = [fileAttributes objectForKey:key];
            NSLog(@"Key : %@ for value : %@", key, value);
      }
}
//删除文件
- (void)deleteFile {
      NSString *documentsPath = [self dirDoc];
      NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
      BOOL res = [fileManager removeItemAtPath:testPath error:nil];
      if (res) {
            NSLog(@"文件删除成功");
      }else {
            NSLog(@"文件删除失败");
      }
}
- (void)applicationWillResignActive:(UIApplication *)application {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
